package com.neegix.system.user.application.service.impl;

import com.neegix.application.command.BaseService;
import com.neegix.exception.BusinessRuntimeException;
import com.neegix.system.user.application.cqrs.command.BatchDeleteUserCommand;
import com.neegix.system.user.application.cqrs.command.BindRolesCommand;
import com.neegix.system.user.application.cqrs.command.NewUserCommand;
import com.neegix.system.user.application.cqrs.command.UpdateUserCommand;
import com.neegix.system.user.application.cqrs.query.UserQueryRepository;
import com.neegix.system.user.application.service.UserService;
import com.neegix.system.user.domain.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-12-03 11:47:28
  */

@Service
public class UserServiceImpl extends BaseService implements UserService{
    @Autowired
    private UserQueryRepository userQueryRepository;

    @Override
    public Void createUser(UserEntity userEntity) {
        return commandInvoker.execute(new NewUserCommand(userEntity));
    }

    @Override
    public Void modifyUser(UserEntity userEntity) {
        return commandInvoker.execute(new UpdateUserCommand(userEntity));
    }

    @Override
    public Void removeUser(List<Long> ids) {
        Integer count = userQueryRepository.selectCount(ids);
        if (count.equals(0)){
            throw new BusinessRuntimeException("要删除的记录不存在！");
        }
        return commandInvoker.execute(new BatchDeleteUserCommand(ids));
    }

     @Override
     public void bindRoles(List<Long> userIds, List<Long> roleIds) {
         commandInvoker.execute(new BindRolesCommand(userIds, roleIds));
     }

    @Override
    public Set<Long> getRolesByPkUser(Long pkUser) {
        return userQueryRepository.getRolesByPkUser(pkUser);
    }
}
